package utils

import (
	"JudgeSandBox/common"
	"fmt"
	"os"
	"time"
)

func PathExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

func PrintToLog(log ...interface{}) {
	//只写，追加，不存在就创建
	//file, _ := os.OpenFile(common.Log, syscall.O_WRONLY|syscall.O_APPEND|syscall.O_CREAT, 0666)
	time := time.Now().Format("2006-01-02 15:04:05")
	fmt.Fprintln(common.LogFile, time, log)
}

// Trim 去掉前后空格与回车
func Trim(str string) string {
	l := 0
	r := len(str)
	for l < len(str) {
		if str[l] == ' ' || str[l] == '\n' {
			l++
		} else {
			break
		}
	}
	for r > 0 {
		if str[r-1] == ' ' || str[r-1] == '\n' {
			r--
		} else {
			break
		}
	}
	return str[l:r]
}
